<?php
class AdsForm extends CFormModel{
	
	public $title;
	public $link;
	public $description;
	public $image;
//	public $newImage;
	public $uploadImage;
	public $noImage;
	public $x;
	public $y;
	public $w;
	public $h;
	
	public $allow_fb;
//	public $fb_diff;
//	public $fb_link;
	public $allow_tw;
//	public $tw_diff;
//	public $tw_link;
	public $allow_md;
//	public $md_diff;
//	public $md_link;
	public $allow_embed;
	
	public $default_link;
	public $cpuv;
	public $fromDate;
	public $toDate;
	public $all_time;
//	public $from_hour;
//	public $to_hour;
	
	public $quiz_able;
	public $quiz;
	public $answer=array();
	public $keyword_able;
	public $keyword=array();

	public function init(){
		$this->allow_fb = 1;
		$this->allow_tw = 1;
		$this->allow_md = 1;
		$this->allow_embed = 1;
		$this->default_link ='adby.me';
		$this->all_time = 1;
		$this->fromDate = date('Y-m-d');
		parent::init();
	}
	
	public function rules()
	{
		return array(
			array('title, link, description', 'required'),
			array('title', 'length', 'max'=>8),
			array('description', 'length', 'max'=>100),
			array('link','url'),
			array('noImage','boolean'),
			array('uploadImage','file','types'=>'jpg,png','maxSize'=>'524288','allowEmpty'=>($this->noImage||!$this->image)),
			array('image','safe'),
			array('x,y,w,h','numerical','integerOnly'=>true,'allowEmpty'=>$this->noImage),
			
			array('allow_fb,allow_tw,allow_md,allow_embed','boolean'),
			//array('allow_fb,fb_diff,allow_tw,tw_diff,allow_md,md_diff,allow_embed','boolean'),
			//array('fb_link','captcha','allowEmpty'=>!$this->fb_diff),
			//array('tw_link','captcha','allowEmpty'=>!$this->tw_diff),
			//array('md_link','captcha','allowEmpty'=>!$this->md_diff),
			
			array('default_link,cpuv,','required'),
			array('default_link','in','range'=>array('adby.me','bit.ly','goo.gl')),
			array('cpuv','in','range'=>array(0.1,0.2,0.3,0.4,0.5,0.6,0.7)),
			array('all_time','boolean'),
			array('fromDate,toDate','date','format'=>'yyyy-mm-dd','allowEmpty'=>$this->all_time),
			
			array('quiz_able,keyword_able','boolean'),
			array('answer,keyword','safe'),
			array('quiz','length','max'=>50,'allowEmpty'=>!$this->quiz_able),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'title'=>Yii::t('adv','title'),
			'link'=>Yii::t('adv','link'),
			'description'=>Yii::t('adv','description'),
			'uploadImage'=>Yii::t('adv','uploadImage'),
			'noImage'=>Yii::t('adv','noImage'),
			'allow_fb'=>Yii::t('adv','allow fb'),
			'allow_tw'=>Yii::t('adv','allow tw'),
			'allow_md'=>Yii::t('adv','allow md'),
			'allow_embed'=>Yii::t('adv','allow embed'),
			'default_link'=>Yii::t('adv','default_link'),
			'cpuv'=>Yii::t('adv', 'cpuv'),
			'period'=>Yii::t('adv', 'period'),
			'all_time'=>Yii::t('adv', 'all_time'),
			'quiz_able'=>Yii::t('adv', 'quiz_able'),
			'keyword_able'=>Yii::t('adv', 'keyword_able'),
		);
	}
	
	public function search($details){
		$array = json_decode($details,true);
		$this->attributes = $array;
		return $this;
	}
	
	public function cropImage(){
		if($this->noImage) return turn;
		$name_image = basename($this->image);
		$name_array= explode('.',$name_image);
		$temp_image = Yii::app()->params['tmpImg_path'].$name_image;
		$adv_image =  Yii::app()->params['adsImg_path'].$name_array[0].'-'.$this->w.'-'.$this->h.'.png';
		try {
			$image = Yii::app()->image->load($temp_image);
			$image->crop($this->w,$this->h,$this->y,$this->x);
			$image->save($adv_image);
//			unlink($temp_image);
			$this->image = basename($adv_image);
			return true;
		}catch(Exception $e){
			return false;
		}
	}
	
	public function  redefineParams(){
//		$this->fb_link = $this->fb_diff?$this->fb_link:null;
//		$this->tw_link = $this->tw_diff?$this->tw_link:null;
//		$this->md_link = $this->md_diff?$this->md_link:null;
		$this->fromDate = !$this->all_time?$this->fromDate:null;
		$this->toDate = !$this->all_time?$this->toDate:null;
		if(!$this->quiz_able){
			$this->quiz = null;
			$this->answer=array();
		}else{
			foreach($this->answer as $key=>$item){
				if($item==null || $item=='') unset($this->answer[$key]);
			}
		}
		if(!$this->keyword_able){
			$this->keyword=array();
		}else{
			foreach($this->keyword as $key=>$item){
				if($item==null || $item=='') unset($this->keyword[$key]);
			}
		}
			
		return true;
	}
}